<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Attachment_model extends My_Model {

	public function __construct()
    {
        parent::__construct();
        $this->model_name="上传附件";
        $this->tablename="attachment";  
	}
	
	public function getattachmentbyid($id){
		$where=["id"=>$id];
        $item=$this->get_item_by_where($id);
        if($item){
            return $item;
        }else{
            return false;
        }
    }


    public function addattachment($data){

        if(!isset($data["addtime"])){
            $data["addtime"]=date("Y-m-d H:i:s");
		}
		return $this->add_item($data); 
    }
    
    public function deleteattachmentbyid($id){
        return $this->db->delete($this->table_prefix.$this->tablename, array('id' => $id));
    }

    public function queryattachment($type,$key,$page,$size,$year,$month){

		$where=["type"=>$type];

        $this->db->where("type",$type);
        if($key){
            $this->db->like("attachment",$key);
        }
        if($year>0 && $month>0){
			$where[]=["year(addtime)"=>$year];
			$where[]=["month(addtime)"=>$month];
		}
		$this->db->where($where);
        $this->db->from($this->table_prefix.$this->tablename);
        $total=$this->db->count_all_results();

        $offset=$size*($page-1);
        if($key){
            $this->db->like("attachment",$key);
        }
        $query=$this->db->where($where)->from($this->table_prefix.$this->tablename)->limit($size, $offset)->order_by("id","desc")->get();
        $data=$query->result_array();

		return array("data"=>$data,"total"=>$total);
    }
}
